<?php

namespace app\admin\logic;

use app\common\model\User;

/**
 * Class UserLogic
 * @package app\admin\logic
 *
 * 用户管理
 * 用户登陆
 */
class UserLogic
{
    /*
     * 用户登陆
     */
    function login($lname, $pwd)
    {
        $user = User::where("lname", $lname)->find();
        if ($user && $user["pwd"] == md5($pwd)) {
            $user["pwd"] = null;
            return $user;
        } else {
            return null;
        }
    }

    function chkPwd($pwd)
    {
        $user = model("User")->where(['id' => session('id'), 'pwd' => md5($pwd)])->find();
        if (!empty($user)) {
            return 1;
        } else {
            return 0;
        }
    }

    function findOne($id)
    {
        return model('user')->find($id);
    }

    /*
     * 条件查询用户
     */
    function query($where)
    {
        return model('user')->with(["Branch","Role"])->where($where)->order(["branch_id","roles","name"])->select();
    }

    /*
     * 新增用户
     */
    function addUser($data)
    {
        $data['pwd'] = md5($data['pwd']);
        if ($data['id'] > 0) {
            return model('user')->data($data)->allowField(true)->isUpdate(true)->save();
        } else {
            return model('user')->data($data)->allowField(true)->save();
        }
    }

    /*
     * 修改用户
     */
    function modifyUser($data)
    {

    }

    function del($id = 0)
    {
        return model('user')->destroy($id);
    }

    public function doModifyPwd($param = '')
    {
        $data = json_decode($param, true);
        return model("User")->where('id', session('id'))->setField('pwd', md5($data['newpwd']));
    }

}
